//
//  Statusview.swift
//  SinaWeibo25
//
//  Created by apple on 16/3/15.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let StatusCellMargin: CGFloat = 10
class Statusview: UIView {
    //声明约束属性  约束对象类型 是 Constraint
    var bottomConstraint: Constraint?
    var status: Status? {
        didSet {
        //绑定模型数据
        iconview.sd_setImageWithURL(status?.user?.headImageURL)
        namelabel.text = status?.user?.name
        mbrank_image.image = status?.user?.mbrank_image
        verified_image.image = status?.user?.verified_type_image
        //TODO: 后期完善  OC中可以使用警告来标示待做事项
//                   sourceLabel.text = status?.source
//                   timelabel.text = status?.created_at
        contentLabel.text = status?.text
            print(status?.imageURls?.count)
            bottomConstraint?.uninstall()
            //给配图视图数据源赋值
            if let urls = status?.imageURls where urls.count != 0 {
                print("原创微博有配图视图")
            pictureview.imageURLs = status?.imageURls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //constraint: 表示获取约束对象
                   self.bottomConstraint = make.bottom.equalTo(pictureview.snp_bottom).offset(StatusCellMargin).constraint
                })

            } else {
                print("原创微博没有++++++++配图视图")
                pictureview.imageURLs = nil
                self.snp_updateConstraints(closure: { (make) -> Void in
                   self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })

            }
    }
}
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUI() {
//        backgroundColor = UIColor.purpleColor()
        addSubview(sepView)
       addSubview(iconview)
       addSubview(mbrank_image)
        addSubview(namelabel)
        addSubview(verified_image)
        addSubview(timelabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        addSubview(pictureview)
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        iconview.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.top.equalTo(sepView.snp_top).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        namelabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconview.snp_top)
            make.left.equalTo(iconview.snp_right).offset(StatusCellMargin)
        }
        mbrank_image.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(namelabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(namelabel.snp_top)
        }
        
        verified_image.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconview.snp_right)
            make.bottom.equalTo(iconview.snp_bottom)
        }
        
        timelabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconview.snp_bottom)
            make.left.equalTo(iconview.snp_right).offset(StatusCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timelabel.snp_top)
            make.left.equalTo(timelabel.snp_right).offset(StatusCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconview.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconview.snp_left)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        iconview.layer.cornerRadius = 18
        iconview.layer.masksToBounds = true
        
        //添加原创微博底部的约束
        self.snp_makeConstraints { (make) -> Void in
           self.bottomConstraint = make.bottom.equalTo(pictureview.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        //添加原创微博底部的约束
        pictureview.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            
          make.size.equalTo(CGSize(width: 100, height: 100))
        }

        


    }
    
    
    private lazy var sepView: UIView = UIView()

    private lazy var iconview: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var mbrank_image: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var verified_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var namelabel: UILabel = UIFactory.labelFactory("25期同志们", textColor: UIColor.darkGrayColor(), fontSize: 15)
    private lazy var timelabel: UILabel = UIFactory.labelFactory("12:12", textColor: UIColor.darkGrayColor(), fontSize: 15)
    private lazy var sourceLabel: UILabel = UIFactory.labelFactory("小明", textColor: UIColor.darkGrayColor(), fontSize: 11)
    private lazy var contentLabel: UILabel = UIFactory.labelFactory("上不上班睡吧睡吧", textColor: UIColor.darkGrayColor(), fontSize: 14)
    
//    配图视图
    private lazy var pictureview: Statuspicture = Statuspicture()
}
